<template>
  <div class="login_container">
    <!-- 登录盒子 -->
    <div class="login_box">
      <!-- LOGO 盒子 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 表单 -->
      <el-form
        :rules="loginRules"
        :model="loginForm"
        label-width="0px"
        class="login_form"
        ref="loginFormRef"
      >
        <!-- 用户名区  -->
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="iconfont icon-user"
          ></el-input>
        </el-form-item>

        <!-- 密码区  -->
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="iconfont icon-3702mima"
          ></el-input>
        </el-form-item>

        <!-- 按钮区  -->
        <el-form-item class="btns">
          <el-button @click="login" type="primary">登录</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import "@/assets/fonts/iconfont.css";
import { loginAPI } from "@/api/index.js";

export default {
  data() {
    return {
      // 登录表单数据对象
      loginForm: {
        username: "admin",
        password: "123456",
      },

      // 登录表单规则对象
      loginRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 15,
            message: "长度在 3 到 15 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入用户密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "长度在6到15个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },

  methods: {
    // 点击重置按钮 重置表单验证
    resetForm() {
      this.$refs.loginFormRef.resetFields();
    },
    // 点击登录按钮 进行表单验证 调用方法返回一个布尔值
    login() {
      this.$refs.loginFormRef.validate(async (result) => {
        // 如果不符合校验规则  就返回 false
        // console.log(result);
        if (!result) return; //如果不符合校验  就return 终止代码的执行
        const loginFormData = { ...this.loginForm };
        const { data: resBody } = await loginAPI(loginFormData);
        if (resBody.meta.status !== 200)
          return this.$msg.error(resBody.meta.msg);
        this.$msg.success("登陆成功啦~~");
        // 登录成功后 保存用户的token值
        window.sessionStorage.setItem("token", resBody.data.token);
        this.$router.push("/home");
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.avatar_box {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
}

img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: #eee;
}

// 表单样式
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
// 按钮区
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>